import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import path from 'path'

import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: ['vue', 'vue-router'],
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  base: '/',
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
      comps: path.resolve(__dirname, 'src/components'),
      styles: path.resolve(__dirname, 'src/styles'),
      views: path.resolve(__dirname, 'src/views'),
      layouts: path.resolve(__dirname, 'src/layouts'),
      utils: path.resolve(__dirname, 'src/utils'),
      apis: path.resolve(__dirname, 'src/apis'),
      assets: path.resolve(__dirname, 'src/assets'),
    },
  },
  // 打包配置
  build: {
    target: 'modules',
    outDir: '../webroot', // 指定输出路径
    emptyOutDir: true,
    assetsDir: 'webroot/assets', // 指定生成静态资源的存放路径
    minify: 'terser', // 混淆器，terser构建后文件体积更小
    chunkSizeWarningLimit: 1000, // 包限制提醒
    rollupOptions: {
      // rollup打包配置
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString()
          }
        },
      },
    },
  },
  // 本地运行配置及代理配置
  server: {
    host: 'localhost',
    port: 8100,
    open: false, // 在服务器启动时自动在浏览器中打开应用程序
    // 代理配置
    proxy: {
      '/api': {
        changeOrigin: true,
        target: 'http://127.0.0.1:9090', // 代理地址 本地环境
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
    },
  },
})
